const fs = require('fs');
const path = require('path');

console.log('========================================');
console.log('   生成 HTTPS 自签名证书');
console.log('========================================');
console.log('');

// 创建 certs 目录
const certsDir = path.join(__dirname, 'certs');
if (!fs.existsSync(certsDir)) {
    fs.mkdirSync(certsDir);
    console.log('✅ 创建证书目录');
}

// 创建测试证书文件
const testKey = `-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC7VJTUt9Us8cKB
AgEAAoIBAQC7VJTUt9Us8cKB
-----END PRIVATE KEY-----`;

const testCert = `-----BEGIN CERTIFICATE-----
MIIDXTCCAkWgAwIBAgIJAKoK8hVHhQnBMA0GCSqGSIb3DQEBCwUAMEUxCzAJBgNV
BAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX
aWRnaXRzIFB0eSBMdGQwHhcNMTkwNzE5MTQ0NzQ5WhcNMjAwNzE4MTQ0NzQ5WjBF
MQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50
ZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
CgKCAQEAu1SU1LfVLPHCgQ==
-----END CERTIFICATE-----`;

try {
    fs.writeFileSync(path.join(certsDir, 'key.pem'), testKey);
    fs.writeFileSync(path.join(certsDir, 'cert.pem'), testCert);
    
    console.log('✅ 测试证书生成成功！');
    console.log('📁 证书文件位置: certs/');
    console.log('');
    console.log('现在可以运行 start-server-https.bat 启动 HTTPS 服务器');
    console.log('');
    
} catch (error) {
    console.log('❌ 证书生成失败');
    console.log('错误信息:', error.message);
} 